package org.example.javase.generics;

import org.junit.Test;

public class GenericsTest {

    /**
     * 测试泛型类
     */
    @Test
    public void test01 () {
        Cage<Bird> cage1 = new Cage<>(new Bird());
        Cage<Tiger> cage2 = new Cage<>(new Tiger());
        Cage<Lion> cage3 = new Cage<>(new Lion());

        cage1.showAnimalInCage();
        cage2.showAnimalInCage();
        cage3.showAnimalInCage();
    }

    /**
     * 测试泛型接口
     */
    @Test
    public void test02 () {
        Generator generator = new GeneratorImpl();
        System.out.println(generator.method());
    }

    /**
     * 测试泛型方法
     */
    @Test
    public void test03 () {
        // 创建不同类型数组： Integer, Double 和 Character
        Integer[] intArray = { 1, 2, 3 };
        String[] stringArray = { "Hello", "World" };
        Util.printArray(intArray);
        Util.printArray(stringArray);
    }
}

class Cage<T extends Animal> {
    private T animal;

    public Cage (T animal) {
        this.animal = animal;
    }

    public void showAnimalInCage() {
        System.out.println(animal);
    }
}

class Animal {

}

class Bird extends Animal {
    @Override
    public String toString() {
        return "Here's a bird!";
    }
}

class Lion extends Animal {
    @Override
    public String toString() {
        return "Here's a lion!";
    }
}

class Tiger extends Animal {
    @Override
    public String toString() {
        return "Here's a tiger!";
    }
}

interface Generator<T> {
    public T method();
}

class GeneratorImpl implements Generator<String>{
    @Override
    public String method() {
        return "hello";
    }
}

class Util {
    public static <E> void printArray(E[] inputArray) {
        for (E element : inputArray) {
            System.out.printf("%s ", element);
        }
        System.out.println();
    }
}
